﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RipPoll.DataAccess;
using RipPoll.Library;

namespace RipPoll.Business
{
    public class UserBO
    {
        public UserPE GetUser(int id)
        {
            RippollEntities re = new RippollEntities();

            IQueryable<UserPE> v = from u in re.Users
                                   where u.Id.Equals(id)
                                   select new UserPE() { BirthYear = (int)u.BirthYear, Email = u.EmailId, Gender = u.Gender, Id = id, City = u.City, ZipCode = u.ZipCode, FirstName = u.FirstName, LastName = u.LastName, PassWord=u.Password, DateAdded = u.DateAdded, DateUpdated = u.DateUpdated, EducationId = u.EducationBackgroundId, ScreenName = u.ScreenName, State = u.State};
            if (v != null && v.Any())
                return v.SingleOrDefault<UserPE>();

            return null;
        }

        public UserPE GetUserForEmail(string emailId)
        {
            RippollEntities re = new RippollEntities();

            IQueryable<UserPE> v = from u in re.Users
                                   where u.EmailId.Equals(emailId)
                                   select new UserPE() { BirthYear = (int)u.BirthYear, Email = u.EmailId, Gender = u.Gender, Id = u.Id, City = u.City, ZipCode = u.ZipCode, FirstName = u.FirstName, LastName = u.LastName, PassWord = u.Password, DateAdded = u.DateAdded, DateUpdated = u.DateUpdated, EducationId = u.EducationBackgroundId, ScreenName = u.ScreenName, State = u.State };
            if (v != null && v.Any())
                return v.SingleOrDefault<UserPE>();

            return null;
        }

        public UserPE GetUserForScreenName(string screenName)
        {
            RippollEntities re = new RippollEntities();

            IQueryable<UserPE> v = from u in re.Users
                                   where u.ScreenName.Equals(screenName)
                                   select new UserPE() { BirthYear = (int)u.BirthYear, Email = u.EmailId, Gender = u.Gender, Id = u.Id, City = u.City, ZipCode = u.ZipCode, FirstName = u.FirstName, LastName = u.LastName, PassWord = u.Password, DateAdded = u.DateAdded, DateUpdated = u.DateUpdated, EducationId = u.EducationBackgroundId, ScreenName = u.ScreenName, State = u.State };
            if (v != null && v.Any())
                return v.SingleOrDefault<UserPE>();

            return null;
        }


        public void CheckUserForScreenNameAndEmailId(string screenName, string emailid, out bool emailExists, out bool screenExists)
        {
            screenExists = false;
            emailExists = false;

            RippollEntities re = new RippollEntities();

            IQueryable<UserPE> v1 = from u in re.Users
                                   where u.ScreenName.Equals(screenName)
                                    select new UserPE() { BirthYear = (int)u.BirthYear, Email = u.EmailId, Gender = u.Gender, Id = u.Id, City = u.City, ZipCode = u.ZipCode, FirstName = u.FirstName, LastName = u.LastName, PassWord = u.Password, DateAdded = u.DateAdded, DateUpdated = u.DateUpdated, EducationId = u.EducationBackgroundId, ScreenName = u.ScreenName, State = u.State };
            if (v1 != null && v1.Any())
                screenExists = true;

            IQueryable<UserPE> v2 = from u in re.Users
                                   where u.EmailId.Equals(emailid)
                                    select new UserPE() { BirthYear = (int)u.BirthYear, Email = u.EmailId, Gender = u.Gender, Id = u.Id, City = u.City, ZipCode = u.ZipCode, FirstName = u.FirstName, LastName = u.LastName, PassWord = u.Password, DateAdded = u.DateAdded, DateUpdated = u.DateUpdated, EducationId = u.EducationBackgroundId, ScreenName = u.ScreenName, State = u.State };
            if (v2 != null && v2.Any())
                emailExists = true;

        }

        public void AddUser(UserPE inUserPE)
        {
            RippollEntities re = new RippollEntities();
            User userDAO = new User() { FirstName = inUserPE.FirstName, LastName = inUserPE.LastName, EmailId = inUserPE.Email, City=inUserPE.City, BirthYear= inUserPE.BirthYear, Gender=inUserPE.Gender, ZipCode = inUserPE.ZipCode, ScreenName = inUserPE.ScreenName, DateAdded = inUserPE.DateAdded, DateUpdated = inUserPE.DateUpdated, Password = inUserPE.PassWord, EducationBackgroundId = inUserPE.EducationId};
            re.AddToUsers(userDAO);
            re.SaveChanges();
            inUserPE.Id = userDAO.Id;

            if (inUserPE.PollInterests != null)
            {
                foreach (UserPollInterestsPE inUserPollInterestsPE in inUserPE.PollInterests)
                {
                    UserPollInterest userPollInterestDAO = new UserPollInterest() { UserId = inUserPE.Id, PollCategoryId = inUserPollInterestsPE.PollCategoryId };
                    inUserPollInterestsPE.UserId = inUserPE.Id;
                    re.AddToUserPollInterests(userPollInterestDAO);
                }
            }
            re.SaveChanges();
        }

        public void UpdateUserForPoll(UserPE inUserPE)
        {
            RippollEntities re = new RippollEntities();
            User usr = (from u in re.Users
                       where u.Id == inUserPE.Id
                       select u).Single<User>();

             usr.FirstName = inUserPE.FirstName;
             usr.LastName = inUserPE.LastName;
             usr.State = inUserPE.State;
             usr.City = inUserPE.City;
             usr.DateUpdated = DateTime.Now;
             re.SaveChanges();
        }


        public List<EducationNamesPE> GetEducationNames()
        {
            RippollEntities re = new RippollEntities();

            List<EducationNamesPE> edNames = new List<EducationNamesPE>();

            IQueryable<EducationNamesPE> v = from eds in re.EducationNames
                                           select new EducationNamesPE() { Id= eds.Id, Description = eds.EducationName1 };

            foreach (EducationNamesPE p in v)
                edNames.Add(p);

            return edNames;
        }

    }
}
